package com.woniuxy.util;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.util
 * @Author: Quan
 * @Date: 2025/4/7 9:29
 */
public class DesensitizationUtils {

	/**
	 * 通用脱敏方法
	 * @param str 原始字符串
	 * @param start 起始保留长度
	 * @param end 结尾保留长度
	 * @param mask 替换字符（如"*"）
	 */
	public static String desensitize(String str, int start, int end, String mask) {
		if (str == null || str.isEmpty()) {
			return str;
		}

		int length = str.length();
		if (start + end >= length) {
			return str; // 不脱敏
		}

		StringBuilder sb = new StringBuilder();
		sb.append(str.substring(0, start));
		for (int i = 0; i < length - start - end; i++) {
			sb.append(mask);
		}
		sb.append(str.substring(length - end));
		return sb.toString();
	}

	// 常用场景的快捷方法
	public static String desensitizeName(String name) {
		return desensitize(name, 1, 1, "*");
	}

	public static String desensitizeIdCard(String idCard) {
		return desensitize(idCard, 3, 4, "*");
	}

	public static String desensitizePhone(String phone) {
		return desensitize(phone, 3, 4, "*");
	}
}